package com.mt.skyhoist.util;

import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpHelper {

    public void sendJsonPost(final String content , final String apiUrl,final HttpListener tHttpListener) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                BufferedReader reader = null;
                try {
                    String urlPath = apiUrl;
                    URL url = new URL(urlPath);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.setUseCaches(false);
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("Charset", "UTF-8");
                    // 设置文件类型:
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
                    // 设置接收类型否则返回415错误
                    //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型，以此来防范返回415;
                    conn.setRequestProperty("accept", "application/json");
                    // 往服务器里面发送数据
                    if (content != null && !TextUtils.isEmpty(content)) {
                        // 设置文件长度
                        OutputStream outwritestream = conn.getOutputStream();
                        outwritestream.write(content.getBytes());
                        outwritestream.flush();
                        outwritestream.close();
                    }
                    if (conn.getResponseCode() == 200) {
                        reader = new BufferedReader(
                                new InputStreamReader(conn.getInputStream()));
                        tHttpListener.onSuccess("{\"returnCode\":\"S\",\"returnData\":"+reader.readLine()+"}");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    tHttpListener.onFail("{\"returnCode\":\"F\",\"returnData:\"\""+e.getMessage()+"\"}");
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }


    public void sendCalibrationJsonPost(final HttpListener tHttpListener){
        try {
            String apiUrl = "http://apk.vita-course.com/darts/nibp/calibrate";
            StringBuilder sb = new StringBuilder();
            sb.append("sex=1");
            String device_id = "Abc124";
            sb.append("&time="+System.currentTimeMillis());
            sb.append("&weight=55");
            sb.append("&height=170");
            sb.append("&sys=180");
            sb.append("&dia=120");
            sb.append("&age=35");
            sb.append("&pm_size=64");
            sb.append("&pm=0.93288386,1425.0,955.0,0.425,0.408,0.846,21.0,0.07,303.0,13782.0,100498.0,72.0,0.226,42.0,0.142,0.029,0.732,0.39,0.665,9.559,7.07327,-0.8556661,7.8022094,-0.9597203,-0.1672359,0.20048887,3.0445225,-2.65926,5.7137327,-0.073646516,4.276666,-1.4872203,11.255255,3.7376697,-1.9519281,-0.16369613,9.364948,-3.5404594,-0.31197476,-0.94160855,-0.4079682,0.67803353,5.768321,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,62.0,62.0");
            sb.append("&rver=4");
            sb.append("&datasource=416");
            sb.append("&device_id="+device_id);
            sb.append("&key=c9245f926e3943ff9e08818f6a779006");
            sendJsonPost(sb.toString(),apiUrl,tHttpListener);
        }catch (Exception e){
            e.getStackTrace();
            tHttpListener.onFail("{\"returnCode\":\"F\",\"returnData:\"\""+e.getMessage()+"\"}");
        }
    }
    public interface HttpListener{
        public void onFail(String returnData);
        public void onSuccess(String returnData);
    }
}
